package com.sgcc.pda.bluetooth.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.sgcc.pda.bluetooth.utils.DisplayUtil;
import com.sgcc.pda.mdrh.task.lib_tools_bluetool.R;

/**
 * 描 述：等待提示框 例如：上传工单等待框
 * 作 者：yangzhi
 * 时 间：2020/4/13 10:02
 * 版 本：1.0
 * 版 权： Copyright (c) remarkables-united.Co.Ltd. All rights reserved.
 */
public class LoadingDialog extends Dialog {

    //动画
    private static Animation operatingAnim;
    private static ImageView imageView;

    private LoadingDialog(Context context, int imgResId) {
        super(context, imgResId);
    }

    public static class Builder {
        private Context mContext;
        private int imgResId;//图片资源
        private String msg;//提示文字信息
        private float textSize;//文字大小
        private float marginTop;//上间距
        private int themeColor;//主题颜色
        private int textColor;//文字颜色

        private boolean isCancelable = false;//设置是否可以按返回键取消
        private boolean isCancelOutside = false;//设置点击外部是否取消


        public Builder(Context mContext) {
            this.mContext = mContext;
            this.imgResId = mContext.getResources().getIdentifier("loading1", "drawable", mContext.getPackageName());
            themeColor = ContextCompat.getColor(mContext, R.color.sgcc_green);
            textColor = ContextCompat.getColor(mContext, R.color.sgcc_green);
            textSize = mContext.getResources().getDimension(R.dimen.sp_14);
            marginTop = mContext.getResources().getDimension(R.dimen.dp_20);
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */

        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        /**
         * 设置点击外部是否取消
         *
         * @param isCancelOutside
         * @return
         */
        public Builder setCancelOutside(boolean isCancelOutside) {
            this.isCancelOutside = isCancelOutside;
            return this;
        }

        /**
         * 设置提示消息内容
         *
         * @param msg 提示内容
         * @return
         */
        public Builder setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        /**
         * 设置文字颜色
         *
         * @return
         */
        public Builder setTextColor(int textColor) {
            this.textColor = textColor;
            return this;
        }

        /**
         * 设置文字大小
         *
         * @return
         */
        public Builder setTextSize(int textSize) {
            this.textSize = textSize;
            return this;
        }

        /**
         * 设置图片和文字之间的间距
         *
         * @return
         */
        public Builder setMarginTop(int marginTop) {
            this.marginTop = marginTop;
            return this;
        }


        public LoadingDialog creat() {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View view = inflater.inflate(R.layout.dialog_loding, null);
            LoadingDialog scanLoadingDialog = new LoadingDialog(mContext, R.style.CustomDialog);
            Window window = scanLoadingDialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
            imageView = view.findViewById(R.id.iv_loading);
            TextView tvMsg = view.findViewById(R.id.tv_msg);
            LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.setMargins(0, (int) marginTop, 0, DisplayUtil.dip2px(mContext, 20));
            tvMsg.setLayoutParams(layout);
            //设置文字颜色
            tvMsg.setTextColor(textColor);
            //设置文字大小
            tvMsg.getPaint().setTextSize(textSize);
            //设置图片主题颜色
            Drawable up = ContextCompat.getDrawable(mContext, imgResId);
            Drawable drawableUp = DrawableCompat.wrap(up);
            DrawableCompat.setTint(drawableUp, themeColor);
            imageView.setImageDrawable(drawableUp);
            Drawable upNew = ContextCompat.getDrawable(mContext, imgResId);
            Drawable drawableUpNew = DrawableCompat.unwrap(upNew);
            DrawableCompat.setTintList(drawableUpNew, null);
            scanLoadingDialog.setContentView(view);
            scanLoadingDialog.setCancelable(isCancelable);
            scanLoadingDialog.setCanceledOnTouchOutside(isCancelOutside);
            operatingAnim = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
            LinearInterpolator lin = new LinearInterpolator();
            operatingAnim.setInterpolator(lin);
            if (!TextUtils.isEmpty(msg)) {
                tvMsg.setText(msg);
            } else {
                tvMsg.setVisibility(View.GONE);
            }
            return scanLoadingDialog;
        }


    }

    @Override
    public void show() {
        super.show();
        //延迟的原因是dialog的onCreate onStart调用的时候UI还没出来 这时候调用动画是不会运行的
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != imageView && null != operatingAnim) {
                    imageView.startAnimation(operatingAnim);
                }
            }
        }, 50);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (null != imageView) {
            imageView.clearAnimation();
        }
    }

}
